﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BoutiquePOS.DAL.DAO;
using BoutiquePOS.DAL.Gateway;
using BoutiquePOS.Manager;

namespace BoutiquePOS.UI.UserUI
{
    public partial class PurchaseReturnEntry : System.Web.UI.Page
    {
        PurchaseReturnDtlsManager aPurchaseReturnDtlsManager = new PurchaseReturnDtlsManager();
        List<PurchaseReturnDtls> purchaseReturnDtlses = new List<PurchaseReturnDtls>();

        private int purchaseId = 0;
        PurchaseReturn purchaseReturn = new PurchaseReturn();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    string dateAsString = DateTime.Now.ToString("yyyy-MM-dd");
                    returnDateTextBox.Value = dateAsString;
                    Session["totalNetAmount"] = 0;
                    Session["ProductList"] = purchaseReturnDtlses;
                    Session["PurchaseReturn"] = 0;
                }
                catch (Exception)
                {
                    throw;
                }
            }   
        }

        public void SearchButton(object sender, EventArgs e)
        {
            LoadGridView();
        }
        private void LoadGridView()
        {
            try
            {
                PurchaseGateway purchaseGateway = new PurchaseGateway();
                int id = purchaseGateway.ReturnPurchaseId(purchaseNoTextBox.Value);
                PurchaseReturnDtlsGateway purchaseReturnDtlsGateway = new PurchaseReturnDtlsGateway();
                List<PurchaseDtls> purchaseDtlses = purchaseReturnDtlsGateway.GetAllPurchaseProduct(id);
                purchaseReturnGridView.DataSource = purchaseDtlses;
                purchaseReturnGridView.DataBind();
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void purchaseReturnGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            purchaseReturnGridView.EditIndex = e.NewEditIndex;
        }
        protected void purchaseReturnGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            purchaseReturnGridView.EditIndex = -1;
        }

        protected void purchaseReturnGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                PurchaseReturnDtls aPurchaseReturnDtls = new PurchaseReturnDtls();
                //PurchaseReturnDtlsManager aPurchaseReturnDtlsManager= new PurchaseReturnDtlsManager();
                Session["PurchaseReturn"] = 1;

                int selectedIndex = e.RowIndex;
                int purchaseId1 = Convert.ToInt32(((Label)purchaseReturnGridView.Rows[selectedIndex].FindControl("lblPurchaseId")).Text);
                int returnQty = Convert.ToInt32(((TextBox)purchaseReturnGridView.Rows[selectedIndex].FindControl("txtReturnQty")).Text);
                int returnFreeQty = Convert.ToInt32(((TextBox)purchaseReturnGridView.Rows[selectedIndex].FindControl("txtFreeReturnQty")).Text);
                int productId = Convert.ToInt32(((Label)purchaseReturnGridView.Rows[selectedIndex].FindControl("lblProductId")).Text);
                decimal tp = Convert.ToDecimal(((Label)purchaseReturnGridView.Rows[selectedIndex].FindControl("lblTradePrice")).Text);
                decimal vat = Convert.ToDecimal(((Label)purchaseReturnGridView.Rows[selectedIndex].FindControl("lblVat")).Text);
                decimal discountPer = Convert.ToDecimal(((Label)purchaseReturnGridView.Rows[selectedIndex].FindControl("lblDiscountPer")).Text);
                //decimal discountReturnAmount = (tp * discountPer) / 100 * returnQty;   //Discount Amount Against Return Qty

                decimal totalReturnAmount = Convert.ToDecimal(Session["totalNetAmount"]);
                totalReturnAmount = totalReturnAmount + (tp * returnQty + vat * returnQty) - (((tp * discountPer) / 100) * returnQty);
                //totalReturnAmount = totalReturnAmount + (tp * returnQty + vat * returnQty) - discountReturnAmount;
                Session["totalNetAmount"] = totalReturnAmount;

                purchaseReturnDtlses = (List<PurchaseReturnDtls>)Session["ProductList"];
                aPurchaseReturnDtls.PurchaseId = purchaseId1;
                aPurchaseReturnDtls.ProductId = productId;
                aPurchaseReturnDtls.ReturnQty = returnQty;
                aPurchaseReturnDtls.ReturnFreeQty = returnFreeQty;
                purchaseReturnDtlses.Add(aPurchaseReturnDtls);
                Session["ProductList"] = purchaseReturnDtlses;

                Session["purchaseId"] = purchaseId1;
                purchaseReturnGridView.EditIndex = -1;
                LoadGridView();
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void SavePurchaseReturn(object sender, EventArgs e)
        {
            try
            {
                messageResult.InnerText = "";
                if (Convert.ToInt32(Session["PurchaseReturn"]) > 0)
                {
                    PurchaseReturn purchaseReturn = new PurchaseReturn();
                    purchaseReturn.PurchaseId = Convert.ToInt32(Session["purchaseId"]);
                    purchaseReturn.PurchaseReturnDate = returnDateTextBox.Value;
                    purchaseReturn.TotalNetAmount = Convert.ToDecimal(Session["totalNetAmount"]);
                    aPurchaseReturnDtlsManager.SavePurchaseReturn(purchaseReturn);

                    int id = aPurchaseReturnDtlsManager.PurchaseReturnId(purchaseReturn.PurchaseId);
                    purchaseReturnDtlses = (List<PurchaseReturnDtls>)Session["ProductList"];
                    aPurchaseReturnDtlsManager.SavePurchaseReturnDtls(id, purchaseReturnDtlses);
                    Session["PurchaseReturn"] = 0;
                }
                else
                {
                    messageResult.InnerText = "You should update the purchase quantity first";
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void CloseButton(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }
        public void ClearButton(object sender, EventArgs e)
        {
            Clear();
        }
        private void Clear()
        {
            purchaseNoTextBox.Value = "";
        }

    }
}